package com.fan.shop.views.bean;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;

import org.apache.log4j.Logger;

import com.fan.shop.businesslogic.UserCreateProcess;
import com.fan.shop.global.ShopGlobalVariables;
import com.fan.shop.services.RoleService;
import com.fan.shop.services.UserService;


@ManagedBean(name = "registerBean")
@RequestScoped
public class RegisterBean  extends ShopBean{

	
	private static Logger logger = Logger.getLogger("RegisterBean");

	@ManagedProperty(value = "#{userService}")
	private UserService userService;

	@ManagedProperty(value = "#{roleService}")
	private RoleService roleService;

	private String username;
	private String password;
	private String confirmPassword;
	private String email;
	private String address;
	private String postAddress;
	private String mobile;
	private String phone;

	public UserService getUserService() {
		logger.debug("RegisterBean: getUserService()");
		return userService;
	}

	public void setUserService(UserService userService) {
		logger.debug("RegisterBean: setUserService()");
		this.userService = userService;
	}

	public RoleService getRoleService() {
		return roleService;
	}

	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}

	public String getUsername() {
		logger.debug("RegisterBean: getUsername()");
		return username;
	}

	public void setUsername(String username) {
		logger.debug("RegisterBean: setUsername()");
		this.username = username;
	}

	public String getPassword() {
		logger.debug("RegisterBean: getPassword()");
		return password;
	}

	public void setPassword(String password) {
		logger.debug("RegisterBean: setPassword()");
		this.password = password;
	}

	public String getEmail() {
		logger.debug("RegisterBean: getEmail()");
		return email;
	}

	public void setEmail(String email) {
		logger.debug("RegisterBean: setEmail()");
		this.email = email;
	}


	public static Logger getLogger() {
		return logger;
	}

	public static void setLogger(Logger logger) {
		RegisterBean.logger = logger;
	}

	public String getConfirmPassword() {
		return confirmPassword;
	}

	public void setConfirmPassword(String confirmPassword) {
		this.confirmPassword = confirmPassword;
	}

	public String getAddress() {
		logger.debug("RegisterBean: getAddress()");

		return address;
	}

	public void setAddress(String address) {
		logger.debug("RegisterBean: setAddress()");
		this.address = address;
	}

	public String getPostAddress() {
		logger.debug("RegisterBean: getPostAddress()");
		return postAddress;
	}

	public void setPostAddress(String postAddress) {
		logger.debug("RegisterBean: setPostAddress()");
		this.postAddress = postAddress;
	}

	public String getMobile() {
		logger.debug("RegisterBean: getMobile()");
		return mobile;
	}

	public void setMobile(String mobile) {
		logger.debug("RegisterBean: setMobile()");
		this.mobile = mobile;
	}

	public String getPhone() {
		logger.debug("RegisterBean: getPhone()");
		return phone;
	}

	public void setPhone(String phone) {
		logger.debug("RegisterBean: setPhone()");
		this.phone = phone;
	}
	
	public String registerAction() {
		logger.debug("RegisterBean: registerAction()");
		
		if(username == null || username.length()==0)
		{
			return "register.faces";
		}
		
		
		UserCreateProcess createProcess = new UserCreateProcess();
		createProcess.setUsername(username);
		createProcess.setPassword(password);
		createProcess.setEmail(email);
		createProcess.setAddress(address);
		createProcess.setPostAddress(postAddress);
		createProcess.setPhone(phone);
		createProcess.setMobile(mobile);
		createProcess.setUserService(userService);
		createProcess.setRoleService(roleService);
		
		int result = createProcess.process();
		
		if(result == ShopGlobalVariables.FAILED)
		{
			return "register.faces";
		}
		else
		{
			return "login";
		}
	}
	
	public String getUserAvailability()
	{
		if(userService == null || username == null)
		{
			return "";
		}
		if(userService.hasUser(username))
		{
			return "User: " + username + " already exists!";
		}
		else
		{
			return "";
		}
	}
}
